package com.scujcc.sendemail.Server.Impl;

import com.scujcc.sendemail.Server.SendMail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

//@ConfigurationProperties("spring.mail")
@Service
public class SendMailServerImpl implements SendMail {

    //发送人
    private String username="1649702256@qq.com";
    //收件人
    private String sendTo="xuyuan@cdjcc.edu.cn";
    //标题
    private String subject="Hello";
    //支持发送HTML格式内容
    private String Text="<a href='https://space.bilibili.com/444112452/favlist?fid=1564199552&ftype=create'>点击跳转bilibili</a><img src='https://ts1.cn.mm.bing.net/th/id/R-C.e99d3750f15a470118d0471f62f896a9?rik=ugSM4gEat13aQw&riu=http%3a%2f%2fimage.bbs.bamengame.com%2febf995cc-1804-4c45-88e1-d4002dc8b928.jpg&ehk=Yt5rE%2fwwC%2b3GFHoIjwBtQ4w1ltwsY0rqz5%2bmkQ6uvaU%3d&risl=&pid=ImgRaw&r=0'/>";


    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void sendMail() {

     /*   //简单邮件的发送方式
        SimpleMailMessage meil =new SimpleMailMessage();
        //发送人,可以添加别名
        meil.setFrom(username+"(小可爱)");
        //接受邮箱
        meil.setTo(sendTo);
        //标题
        meil.setSubject(subject);
        //正文类容
        meil.setText(Text);
        //设置发送日期
        Date date = new Date();
        date.setSeconds(10);
        meil.setSentDate(date);
        javaMailSender.send(meil);*/



        //可实现发送附件、图片等邮件
        MimeMessage mimeMailMessage = javaMailSender.createMimeMessage();

        try {
            //将multipart设置为true才能添加附件
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage,true);
            //发送人,可以添加别名
            mimeMessageHelper.setFrom(username+"(小可爱)");
            //接受邮箱
            mimeMessageHelper.setTo(sendTo);
            //标题
            mimeMessageHelper.setSubject(subject);
            //正文类容,通过HTML方式显示
            mimeMessageHelper.setText(Text,true);
            //添加附件
            File file=new File("D:\\LenovoSoftstore\\Install\\spring\\apache-maven-3.8.4\\repository\\com\\alibaba\\druid\\1.2.8\\druid-1.2.8.jar");
            //添加图片
            File file1 = new File("D:\\LenovoSoftstore\\Install\\IdeaProjects\\SpringBoot2\\sendEmail\\src\\main\\resources\\static\\head.jpeg");

            mimeMessageHelper.addAttachment(file.getName(),file);
            mimeMessageHelper.addAttachment("可爱头像.jpg", file1);
            javaMailSender.send(mimeMailMessage);


        } catch (MessagingException e) {
            e.printStackTrace();
        }


    }
}
